package com.stx.friends.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import com.stx.friends.dao.CategoryDao;
import com.stx.friends.entity.CategoryEntity;
import com.stx.friends.vo.CategoryVo;

public class CategoryDaoImpl extends JdbcDaoSupport implements CategoryDao {
	public List<CategoryEntity> list(CategoryVo vo) {

		String sql = "select * from wrx_category";

		JdbcTemplate jt = getJdbcTemplate();

		List<CategoryEntity> categoryList = jt.query(sql,
				new CategorytRowMapper());

		return categoryList;
	}

	class CategorytRowMapper implements RowMapper {
		@Override
		public Object mapRow(ResultSet rs, int rowNum) throws SQLException {

			CategoryEntity entity = new CategoryEntity();

			entity.setCid(rs.getString("cid"));
			entity.setCname(rs.getString("cname"));
			entity.setCreateTime(rs.getString("createTime"));
			entity.setWrxUid(rs.getString("wrxUid"));

			return entity;
		}
	}

	@Override
	public void delCategory(String cid) {

		JdbcTemplate jt = getJdbcTemplate();

		String sql = "delete from wrx_category where cid = ?";

		// 构建参数数组
		Object[] args = { cid };

		jt.update(sql, args);

	}

	@Override
	public void addCategory(CategoryVo category) {

		JdbcTemplate jt = getJdbcTemplate();

		String sql = "insert into wrx_category(cid,cname,createTime,wrxUid) values(seq_wrx_category.nextval,?,?,?)";

		// 构建参数数组
		Object[] args = { category.getCid(), category.getCname(),
				category.getCreateTime(), category.getWrxUid() };

		jt.update(sql, args);

	}

	@Override
	public java.util.List<CategoryEntity> List(CategoryVo vo) {
		// TODO Auto-generated method stub
		return null;
	}
}
